﻿var wpCookies={
		each:function(d,a,c){
				var e,b;
				if(!d){return 0}
				c=c||d;
				if(typeof(d.length)!="undefined"){
					for(e=0,b=d.length;e<b;e++){
						if(a.call(c,d[e],e,d)===false){return 0}
					}
				}else{
					for(e in d){
						if(d.hasOwnProperty(e)){
							if(a.call(c,d[e],e,d)===false){return 0}
						}
					}
				}return 1
		},
		getHash:function(b){
			var c=this.get(b),a;
			if(c){
				this.each(c.split("&"),
				function(d){
					d=d.split("=");
					a=a||{};
					a[d[0]]=d[1]
				})
			}
			return a
		},
		setHash:function(b,c,a,f,d,e){
			var g="";
			this.each(c,function(i,h){
				g+=(!g?"":"&")+h+"="+i
			});
			this.set(b,g,a,f,d,e)
		},
		get:function(c){
			var d=document.cookie,g,f=c+"=",a;
			if(!d){return}
			a=d.indexOf("; "+f);
			if(a==-1){
				a=d.indexOf(f);
				if(a!=0){return null}
			}else{
				a+=2
			}
			g=d.indexOf(";",a);
			if(g==-1){
				g=d.length
			}
			return decodeURIComponent(d.substring(a+f.length,g))
		},
		set:function(b,e,a,g,c,f){
			var h=new Date();
			if(typeof(a)=="object"&&a.toGMTString){
				a=a.toGMTString()
			}else{
				if(parseInt(a,10)){
					h.setTime(h.getTime()+(parseInt(a,10)*1000));
					a=h.toGMTString()
				}else{a=""}
			}
			
			document.cookie=b+"="+encodeURIComponent(e)+((a)?"; expires="+a:"")+((g)?"; path="+g:"")+((c)?"; domain="+c:"")+((f)?"; secure":"")
		},
		remove:function(a,b){
			this.set(a,"",-1000,b)
		}
};


function getUserSetting(a,b){
	var c=getAllUserSettings();
	if(c.hasOwnProperty(a)){return c[a]}
	if(typeof b!="undefined"){return b}
	return ""
}

function setUserSetting(c,f,b){
	if("object"!==typeof userSettings){return false}
	var d="wp-settings-"+userSettings.uid,
		e=wpCookies.getHash(d)||{},
		g=userSettings.url,
		h=c.toString().replace(/[^A-Za-z0-9_]/,""),
		a=f.toString().replace(/[^A-Za-z0-9_]/,"");
	if(b){delete e[h]}
	else{e[h]=a}
	
	wpCookies.setHash(d,e,31536000,g);
	wpCookies.set("wp-settings-time-"+userSettings.uid,userSettings.time,31536000,g);return c
}

function deleteUserSetting(a){
	return setUserSetting(a,"",1)
}

function getAllUserSettings(){
	if("object"!==typeof userSettings){
		return{}
	}
	return wpCookies.getHash("wp-settings-"+userSettings.uid)||{}
};
function convert_accent(_string){

	var nomarkKeyA=new RegExp('[Ă,Â,Á,À,Ả,Ã,Ạ,Ắ,Ằ,Ẳ,Ẵ,Ặ,Ấ,Ầ,Ẩ,Ẫ,Ậ]','g');
	var nomarkKeya=new RegExp('[ă,â,á,à,ả,ã,ạ,ắ,ằ,ẳ,ẵ,ặ,ấ,ầ,ẩ,ẫ,ậ,ä]','g');
	var nomarkKeyE=new RegExp('[Ê,É,È,Ẻ,Ẽ,Ẹ,Ế,Ề,Ể,Ễ,Ệ]','g');
	var nomarkKeye=new RegExp('[ê,é,è,ẻ,ẽ,ẹ,ế,ề,ể,ễ,ệ,ë]','g');
	var nomarkKeyO=new RegExp('[Ô,Ơ,Ó,Ò,Ỏ,Õ,Ọ,Ố,Ồ,Ổ,Ỗ,Ộ,Ớ,Ờ,Ở,Ỡ,Ợ]','g');
	var nomarkKeyo=new RegExp('[ô,ơ,ó,ò,ỏ,õ,ọ,ố,ồ,ổ,ỗ,ộ,ớ,ờ,ở,ỡ,ợ,ö]','g');
	var nomarkKeyU=new RegExp('[Ư,Ú,Ù,Ủ,Ũ,Ụ,Ứ,Ừ,Ử,Ữ,Ự]','g');
	var nomarkKeyu=new RegExp('[ư,ú,ù,ủ,ũ,ụ,ứ,ừ,ử,ữ,ự,ü]','g');
	var nomarkKeyI=new RegExp('[Í,Ì,Ỉ,Ĩ,Ị]','g');
	var nomarkKeyi=new RegExp('[í,ì,ỉ,ĩ,ị,ï,î]','g');
	var nomarkKeyY=new RegExp('[Ý,Ỳ,Ỷ,Ỹ,Ỵ]','g');
	var nomarkKeyy=new RegExp('[ý,ỳ,ỷ,ỹ,ỵ,ÿ]','g');
	var nomarkKeyc=new RegExp('[ç]','g');
	var nomarkKeyC=new RegExp('[Ç]','g');
	var nomarkKeyD=new RegExp('[Đ]','g');
	var nomarkKeyd=new RegExp('[đ]','g');
	var nomarkKeyd=new RegExp('[d]','g');
	var nomarkKeyOther=new RegExp('[!-/,:,\[-^,`,{-~]','g'); 
	
	
	
	_string=$.trim(_string);
	_string=_string.replace(nomarkKeyOther,' ');
	_string=_string.replace(/\s+/g," ");
	_string=_string.replace(/ /g,'-');
	_string=_string.replace(nomarkKeyA,'A');
	_string=_string.replace(nomarkKeya,'a');
	_string=_string.replace(nomarkKeyE,'E');
	_string=_string.replace(nomarkKeye,'e');
	_string=_string.replace(nomarkKeyO,'O');
	_string=_string.replace(nomarkKeyo,'o');
	_string=_string.replace(nomarkKeyU,'U');
	_string=_string.replace(nomarkKeyu,'u');
	_string=_string.replace(nomarkKeyI,'I');
	_string=_string.replace(nomarkKeyi,'i');
	_string=_string.replace(nomarkKeyY,'Y');
	_string=_string.replace(nomarkKeyy,'y');
	_string=_string.replace(nomarkKeyc,'c');
	_string=_string.replace(nomarkKeyC,'C');
	_string=_string.replace(nomarkKeyD,'D');
	_string=_string.replace(nomarkKeyd,'d');
	return _string;
} 
function checkUrl(url_friendly){
	$.post("category/admin/checkURL", { url: url_friendly})
	 .done(function(data) {
		 $('#ajax-response').html(data);
	});
}
$(document).ready(function() {
	
	$("li.wp-has-submenu").hover(
		function () {
			$(this).addClass("opensub");
		},
		function () {
			$(this).removeClass("opensub");
		}
	);
	$("#tag-name").change(function() {
		url_friendly=convert_accent(this.value);
		$(this).parents('form').find('#tag-slug').val(url_friendly);
		//checkUrl(url_friendly);
	});
	$("#collapse-menu").on("click.collapse-menu",function(l){
		var k=$(document.body);
		
			$("#adminmenu div.wp-submenu").css("margin-top","");
		if($(window).width()<900){
			if(k.hasClass("auto-fold")){
				k.removeClass("auto-fold");
				k.removeClass("folded");
			}else{
				k.addClass("auto-fold");
			}
		}else{
			if(k.hasClass("folded")){
				k.removeClass("folded");
			}else{
				k.addClass("folded");
			}
		}
	});
	 
	$(".cancel").live("click",function(){
		$(this).parents('tr').prev().show();
		$(this).parents('tr').remove();
	});
	$(".editinline").live("click",function(){
		selectorInfo=$(this).parents('div.row-actions').next();
		$id=selectorInfo.attr('id').replace('inline_', "");
		$name=selectorInfo.find(".name").text();
		$slug=selectorInfo.find(".slug").text();
		$parent=selectorInfo.find(".parent").text();
		$($('#inlineedit').html()).insertAfter($(this).parents('tr'));
		selectorNewTr=$(this).parents('tr').next();
		selectorNewTr.find('input[name="name"]').val($name);
		selectorNewTr.find('input[name="slug"]').val($slug);
		selectorNewTr.find('input[name="id"]').val($id);
		selectorNewTr.find('input[name="parent"]').val($parent);
		$('.inline-edit-row').show();
		$(this).parents('tr').hide();
	});

	$(".editinlineAdminMenu").live("click",function(){
		selectorInfo=$(this).parents('div.row-actions').next();
		$id=selectorInfo.attr('id').replace('inline_', "");
		$name=selectorInfo.find(".name").text();
		$type=selectorInfo.find(".parent").text();
		$href=selectorInfo.find(".href").text();
		$class=selectorInfo.find(".class").text();
		$icon=selectorInfo.find(".icon").text();
		$($('#inlineedit').html()).insertAfter($(this).parents('tr'));
		selectorNewTr=$(this).parents('tr').next();
		selectorNewTr.find('input[name="name"]').val($name);
		selectorNewTr.find('select[name="parent"]').val($type);
		selectorNewTr.find('input[name="id"]').val($id);
		selectorNewTr.find('input[name="class"]').val($class);
		selectorNewTr.find('input[name="href"]').val($href);
		selectorNewTr.find('input[name="icon"]').val($icon);
		$('.inline-edit-row').show();
		$(this).parents('tr').hide();
	});
	$(".delPhoto").live("click",function(){
		$id=$(this).attr('id');
		$(this).prev().animate({ opacity: 0.3 }, 800);
		$('<input type="hidden" name="delPhoto[]" value="'+$id+'" />').insertAfter($(this));
		$(this).remove();
	});
});